/* ***** BEGIN LICENSE BLOCK *****
 * FW4SPL - Copyright (C) IRCAD, 2009-2010.
 * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
 * published by the Free Software Foundation.
 * ****** END LICENSE BLOCK ****** */

#include <list>
#include <algorithm>

#include "fwData/visitor/accept.hpp"
#include "fwData/visitor/BreathFirst.hpp"

namespace fwData
{
namespace visitor
{

//------------------------------------------------------------------------------

BreathFirst::BreathFirst() : m_recursionLevel(0)
{
}

//------------------------------------------------------------------------------

BreathFirst::~BreathFirst()
{
}

//------------------------------------------------------------------------------

void BreathFirst::next(::fwTools::Object::sptr src, ::fwTools::Object::ChildContainer &fields)
{
    ++m_recursionLevel;
    // insert ONLY ONCE child in breathFirst order+source
    ::fwTools::Object::ChildContainer::iterator f;
    for (f = fields.begin(); f != fields.end() ; ++f)
    {
        SLM_ASSERT("fwTools::Object dynamicCast failed", ::fwTools::Object::dynamicCast( *f ) );
        std::pair< Source , Child > key( src, ::fwTools::Object::dynamicCast( *f ) );
        if ( std::find( m_fifo.begin(), m_fifo.end(),  key )==m_fifo.end()  )
        {
            // not already inserted
            m_fifo.push_back(key );
        }
    }

    // process list
    if (m_recursionLevel == 1)
    {
        while ( !m_fifo.empty() )
        {
            m_source = m_fifo.front().first;
            Child achild= m_fifo.front().second;
            m_fifo.pop_front();
            accept( achild, this );
        }
    }
    --m_recursionLevel;
}

//------------------------------------------------------------------------------

} // namespace visitor

} // namespace fwData
